$.fn.datagrid.defaults.pageList = [10,20,50, 100,200, 500,1000]; //设置列表数目

$(document).ready(function () {
    $('.full_parent').each(function () {
        var o = this;
        fitSize(o);
        var p = $(window).resize(function () {
            fitSize(o);
        });
    });

    if ($.browser.msie && $.browser.version == "6.0") {
        ie6png(); 
    }

});

//在主界面上增加一个选项卡，或当前窗口不在主界面内，则在新窗口中打开
function mainAddTab(title, url, closable, iconCls) {
    if (window.top.AddTab) {
        window.top.AddTab(title, url, closable, iconCls);
    }
    else {
        Href(url, true);
    }
}

//在窗口上显示各种消息
var show_msg_autohide = null;
function show_msg(msg, icon) {
    if (parent != this && parent.show_msg) {
        parent.show_msg(msg, icon);
    }
    else {
        $(document).ready(function () {
            var msp = $("#show_msg");
            if (msp.length > 0) {
                clearMsg();
            }
            var cssname = 'show_msg';
            if (icon) {
                cssname += " show_msg_icon show_msg_" + icon;
            }
            var html = '<div id="show_msg" class="' + cssname + '">' + msg + '</div>';
            $('body').append(html);
            msp = $("#show_msg");
            msp.css('margin-left', -msp.outerWidth(true) / 2);
            msp.show();
            autoclose();
            msp.mouseenter(function () {
                clearTimeout(show_msg_autohide);
            });
            msp.mouseleave(autoclose);

            function clearMsg() {
                msp.remove();
                clearTimeout(show_msg_autohide);
            }
            function autoclose() {
                show_msg_autohide = setTimeout(function () {
                    msp.fadeOut(200, clearMsg);
                    clearMsg();
                }, 4000);
            }
        });
    }
}

//显未json的消息
function show_json_msg(json) {
    var icon;
    if (json.success) {
        icon = 'success';
    }
    else {
        icon = 'error';
    }
    if (json.message) {
        show_msg(json.message, icon);
    }

}

//显示未转换为json的消息,并返回转换成功的json对象，失败返回 undefine
function show_txt_msg(data) {
    try {
        var ret = eval('(' + data + ')');
        if (ret.success == false && ret.message == "nologin") { //未登陆
            window.location.href = ret.data;
        }
        else {
            show_json_msg(ret);
        }
    }
    catch (e) {
        $(document.createElement('div')).window({
            width: 800,
            height: 480,
            title: e.message,
            content: data
        });
    }
    return ret;
}
//关闭当前窗口
function CloseMe() {
    try {
        if (this.frameElement && this.frameElement.close) {
            this.frameElement.close();
        }
        else if (window.top.CloseCurentPage) {
            window.top.CloseCurentPage();
        }
        else {
            window.close();
        }
    } catch (e) { };
}

//显示隐藏搜索框
function search_click() {
    var layout = $('body>.easyui-layout');
    var panel = layout.layout('panel', 'north');
    if ($('.search_box:visible').length == 0) {
        $('.search_box').show();
    }
    else {
        $('.search_box').hide();
    }
    panel.panel('resize', { height: $('.tools_box').outerHeight(true) + $('.navigation').outerHeight(true) });
    layout.layout('resize');
}

//主界面工具上的搜索
function mainSearch(expr, url) {
    var ps = {};
    $(expr).each(function (i) {
        if (this.name && $(this).val() != undefined && $(this).val() != '')
            ps[this.name] = $(this).val();
    });
    if (url!= undefined) $('#details').datagrid({ url: url, queryParams: ps,pageNumber:1});
    else $('#details').datagrid('load', ps);
    return ps;
}

function boolformat(value, row, index) {
    return value ? '是' : '否';
}

//easyui datagrid 基本的操作格式化
function IDcmdformat(value, row, index) {
    var id = row.ID;
    if (id == undefined) id = row.id;
    return '<a href="javascript:show_fullscreen_dialog(serverUrl.edit + \'/\' + \'' + id + '\',on_close,\'edit\')">修改</a>&nbsp; <a href="javascript:Del(' + id + ',onDeleteData)">删除</a>';
}

//超连接格式化
function hrefformat(url, text, title, closeable, iconCls) {
    var ret = '<a href="javascript:mainAddTab(\'' + title + '\', \'' + url + '\', ' + closeable + ',\'' + (iconCls==null|| iconCls==undefined?'':iconCls) + '\')">' + text + '</a>';
    return ret;
}

function iconformat(value, row, index) {
    var ret = value;
    if (row.iconCls) {
        ret = '<div style="padding-left:18px;" class="' + row.iconCls + '">' + ret + '</div>';
    }
    return ret;
}

function emailFormat(value, row, index) {
    return '<a href="mailto:' + value + '">' + value + '</a>';
}


//删除所选中的所有行
function DeleteSelecteds(selected,key,callback,url) {
    if (selected.length == 0) {
        show_msg('请先选择要删除的数据', 'bulb');
        return;
    }
    if (!key) key = "ID";
    var ids = '';
    for (var i = 0; i < selected.length; i++) {
        ids += ',' + (selected[i][key]);
    }
    ids = ids.substring(1);
    Del(ids, callback, url);
}

//删除指定ID的数据，以逗号分隔多个ID，请确实有serverUrl.del,并接收此参数
function Del(id, callback,url, ps) {
    $.messager.confirm('确认', '确实要删除吗？', function (r) {
        if (r) {
            if (!url) url = serverUrl.del;
            if (!ps) ps = {};
            ps.id = id;
            postModal(url, ps, function (r) {
                if (r.success) {
                    if (callback) callback();
                }
            });
        }
    });
}


//显示全屏的一个窗口
function show_fullscreen_dialog(url,onClosed,key) {
    var ifrmae = $('<iframe class="show_fullscreen_dialog" src="' + url + '" scrolling="auto" frameborder="0"></iframe>');
    ifrmae.appendTo(document.body);
    ifrmae[0].close = function () {
        ifrmae.remove();
        if(onClosed) onClosed(key);
    }
}

//提交表单
function ajax_submit(target, url, fn,param) {
    var vlida = EasyuiForm.validate(target);
    if (vlida == 0) {
        $.messager.progress();
        $(target).AjaxPost(url, function (data) {
            $.messager.progress('close');
            var r = show_txt_msg(data.responseText);
            if (r) {
                if (fn) fn(r);
            }
        }, param);
    }
    else {
        show_msg('有 ' + vlida + ' 项错误', 'error');
    }
}

//ajax 异步请求，并显示遮罩层与结果
function postModal(url, param, fn, isModal) {
    if (isModal == undefined) isModal = true;
    if(isModal) $.messager.progress();
    $('<div/>').AjaxPost(url, function (data) {
        if (isModal) $.messager.progress('close');
        var r = show_txt_msg(data.responseText);
        if (r) {
            if (fn) fn(r);
        }
    }, param);
}


//表格树载入数据
function treeLoadData(node, param) {
    if (node) {
        $(this).treegrid('options').url = serverUrl.list + '/' + node.id;
    } else {
        $(this).treegrid('options').url = serverUrl.list;
    }
}

//在当前窗口中开启一个easyui 窗口
function showWindow(ps) {
     return $('<div/>').window(ps);
 }

 //按字典格式化表格字段
 function dicformat(dic, value) {
     for (var i in dic) {
         var d = dic[i];
         if (d.Code == value) {
             return d.Code + ':' + d.DictionaryName;
         }
     }
 }